home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.csug.rochester.edu!rit!kar
- From: kar@cs.rit.edu (Kenneth A Reek)
- Subject: Re: accessing structures (newbie question)
- Message-ID: <1996Feb20.140043.14285@cs.rit.edu>
- Sender: news@cs.rit.edu (USENET News Admin)
- Nntp-Posting-Host: madrid
- Organization: Rochester Institute of Technology, Rochester, NY
- References: <4g8gic$o6u@news1.sunbelt.net> <3128FA60.5227@metagen.co.uk>
- Date: Tue, 20 Feb 1996 14:00:43 GMT
-
- In article <3128FA60.5227@metagen.co.uk>, Rob Stenton <rws@metagen.co.uk> writes:
- > There are basically two ways of accessing elements within a malloced
- > buffer:
- > 1. Treat the malloced buffer as an array (as you suggested).
- > 2. Increment a local pointer by sizeof(picture) in loop iteration.
- >
- > The first method is preferable because the second method involves pointer
- > arithmetic (always best avoided) which can cause problems on different
- > memory architectures and assumes things about BYTE sizes (but usually
- > works).
-
- The ANSI standard specifies the behavior of pointer arithmetic, so
- there is no problem using it on any specific architecture. Problems caused
- by different sizes of things such as integers can occur if you take the
- data generated on one architecture, byte for byte, and try to interpret it
- on an architecture with different size data. However, a correct program
- on one architecture will also run correctly on the other, even though the
- byte for byte image of the data that is created may differ.
-
- > I think you may have been using the wrong syntax when you tried the array
- > method. The xth item is given by 'photos[x].' which is equivalent to
- > '(photos + x * sizeof(picture))->'.
-
- Conceptually true, but actually incorrect. When pointer arithmetic
- is done, the compiler scales values added to a pointer by the size of the
- thing it points to. The intent of the expression above is actually achieved
- with this expression:
-
- ( photos + x )->
-
- > The two ways for solving this problem are therefore:
- > 1. (Recommended)
- > for (x=0;x<num_of_files;x++)
- > {
- > strncpy(photos[x].filename, filelist[x],12);
- > strncpy(photos[x].diskname, inputdisk,12);
- > strncpy(photos[x].indexname, inputindex,12);
- > }
-
- Straightforward and correct.
-
- > or
- > 2. (Not recommended)
- > for (x=0;x<num_of_files;x++)
- > {
- > strncpy((photos + x * sizeof(picture))->filename,
- > filelist[x],12);
- > strncpy((photos + x * sizeof(picture))->diskname,
- > inputdisk,12);
- > strncpy((photos + x * sizeof(picture))->indexname,
- > inputindex,12);
- > }
-
- Incorrect due to the scaling done by the compiler with pointer
- arithmetic, as described above. But even if that were fixed, this use
- of pointers has no advantage over the subscript example above. To get
- greater efficiency, use a copy of the pointer and increment it:
-
- struct picture *pp; /* or whatever, I don't remember the
- original poster's structure */
- ...
- for( pp = photos; pp < photos + num_of_files; pp++ ){
- strncpy( pp->filename, ... );
- strncpy( pp->diskname, ... );
- strncpy( pp->indexname, ... );
- }
-
- The pp++ in the for statement increments pp by the right amount so that
- it points to the next structure in the array. If speed is your primary
- concern, declare a second pointer variable and initialize it to
-
- photos + num_of_files
-
- and use that in the for statement rather than the expression.
-
-
- Kenneth A. Reek kar@cs.rit.edu
- Rochester Institute of Technology 716-475-6155 (voice)
- Rochester, NY 14623-0887 USA 716-475-7100 (fax)
-